package com.lucian.ge.service.shop.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lucian.ge.dao.shop.CartMapper;
import com.lucian.ge.dao.shop.OrderItemMapper;
import com.lucian.ge.dao.shop.OrderMapper;
import com.lucian.ge.dao.shop.ProductMapper;
import com.lucian.ge.service.shop.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Lucicol on 2019/7/10.
 */
@Service("OrderServicel")
public class OrderServicelmpl implements OrderService {

    @Autowired(required=false)
    private OrderMapper orderMapper;

    @Autowired(required=false)
    private ProductMapper productMapper;

    @Autowired(required=false)
    private OrderItemMapper orderItemMapper;

    @Autowired(required=false)
    private CartMapper cartMapper;

    public void create(Integer userId,String shippingWx, String orderList, String remark) {
        BigDecimal payment = new BigDecimal("0");
        long orderNo = this.generateOrderNo();

        JSONArray objs = JSONObject.parseArray(orderList);
        for(Object o : objs){
            JSONObject oTempbj = (JSONObject)o;
            Map<Object, Object> product = productMapper.selectproOb(Integer.valueOf(oTempbj.get("id").toString()));
            orderItemMapper.addItem(userId,product.get("id"),product.get("price"),oTempbj.get("quantity"),orderNo,product.get("main_image"),product.get("name"));

            cartMapper.delete(userId, Integer.valueOf(oTempbj.get("id").toString()));

            Double price = Double.valueOf(product.get("price").toString());
            Double pr = price.doubleValue();
//            Integer pr = price.intValue();
            Integer qu = Integer.parseInt(oTempbj.get("quantity").toString());
            Double total = pr*qu.doubleValue();
//            Integer total = pr*qu;

            payment = payment.add(BigDecimal.valueOf((Double)total));
//            payment = payment.add(BigDecimal.valueOf((Integer)total));
//            System.out.println("payment"+ payment);
        }
        orderMapper.create(userId, shippingWx, orderNo, payment, remark);
    }

    private long generateOrderNo(){
        long currentTime =System.currentTimeMillis();
        return currentTime+new Random().nextInt(100);
    }

    public List<Map<String, Map<String, String>>>  getOrderList (Integer userId, Integer status) {
        List<Map<String, Map<String, String>>> orderProductList = new ArrayList<Map<String, Map<String, String>>>();
        List<Map<String, String>> orderList = orderMapper.selectList(userId, status);
        for (Map order : orderList) {
            List<Map<String, String>> orderItemList = orderItemMapper.selectList(Long.parseLong(order.get("order_no").toString()));
            order.put("orderPorduct",orderItemList);
            orderProductList.add(order);
        }
        return orderProductList;
    }

    public List<Map<String, String>>  getOrderItemListDetail (Integer userId, String orderNo) {
        List<Map<String, String>> orderItemList = orderItemMapper.selectList(Long.parseLong(orderNo));
        return orderItemList;
    }

    public Map<String, String> getOrderDetail (Integer userId, String orderNo) {
        Map<String, String> orderDetail = orderMapper.selectDetail(Long.parseLong(orderNo));
        return orderDetail;
    }

    public void changeOrderStatus(Integer userId, String orderNo, String status) {
        orderMapper.changeOrderStatus(userId, orderNo, status);
    }

    public void addShareId(Integer userId, String orderNo, String shareId) {
        orderMapper.addShareId(userId, orderNo, shareId);
    }
}
